package com.sosky;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 给你一份『词汇表』（字符串数组） words 和一张『字母表』（字符串） chars。
 * <p>
 * 假如你可以用 chars 中的『字母』（字符）拼写出 words 中的某个『单词』（字符串），那么我们就认为你掌握了这个单词。
 * <p>
 * 注意：每次拼写时，chars 中的每个字母都只能用一次。
 * <p>
 * 返回词汇表 words 中你掌握的所有单词的 长度之和。
 */
public class Solution1160 {
    public static void main(String[] args) {
        System.out.println(countCharacters(new String[]{"jdimdfzbygjgdrsisgblztowvwzewvrwuizaihnctoonlykrbzncqvbzjahxvyswdzcbwaqliiyzbddtsxrwb", "ybdhccgznflezzvifoshbatwedohwwhyajylprerarbsjcll", "febacfxkrwnkyoyhykzbfuueohtkvqjkrmclmiinmpwhumaummpxjqmvqnvlwkmiizewfnqnmjqmnxacrhgxodyglvzyyii", "tcymwrytgfnbggzqptaqwzpkjhleisuwsfzhna", "rdvuvrrqerihadnurdfkzfiwtodnzcwvrmuwkyjhgkdjtnuwlclnhttidukyhfdzozix", "mjoryzsbvkqlmtmcmsjljloabfaliecpmmiumq", "iwzpczbknmqrankiypxjxthlzsvhchpqv", "dqpgxyspa", "igsvidvyonqyprvkoaphuvqaxcceefckypojgfdiyubagihkztnhindjcdpolt", "iamvwzvdbfonuurfdzcnvohqrpyxgmjvbyiyovvccsilkblgpojmwvhdsewldgzvdlnecfudjjkmgmil", "lbtjpakidwtavvtllcpekgcmqigmrfkvfjyzbxlpxtnklqrfoasnocjnlhzafcjqdjqgmuardsifyrlkvrndat", "tfqdhgvduounzgbdnipofenkqzqrlntcppwkk", "dwtuwpqugxvkjnhxvpxsdxyeurkldhxnpmicxmntiokhcclptbbbamzcrwachvzi", "pdnmdakvxnwdmibwqclesycgomztyowtjskvwiwuaumwubrmkli", "sxabmaciwtbbtwwawfhqisthjqxsxslsptcxqgsrzxiirxmzzmoulqmwzwatkrjqgpelkenulstuxwzjno", "enhtapuuyildahjyryubmjqdhbtfhjzhwppqbzriytu", "momfgjhyxzpsp", "hhdyxizwufxjewqvbforsauqgexpqgrhbmycwcaxbwldljuhclpyfawbhypinsfjepgded", "cgowwwhvlaziuptfsxqygxxnqxbregcwaodyklkkywpdtofrupmeeaeoemmpdwltdzbvebuzfumfdbdqu", "xspywgpzetvfdzeyterumsweldzsdarwshssaxlfgqqlckcyeealjqdfkjdncoivvgkmcvaod", "ubuubimfvxvplgikclwilovjdkns", "luzrzwfwzlunkwlvpjqpwckboxhgufsjlpmklxxtiuvlvaydkdvqumhiospabiemygntpfotocufttzzewkbxf", "mqqktykhjxtebccvpwttybgvkkxndvxfagdnjmbezlmdgw", "opcobbidwttpeisekgmowtgcozeinvfliqbkbrtkvwxdobavjyrknsadjijwybtbayfytxeotfp", "c", "cefqqjft", "ijavryhszfeevxvrcubkvpobxajzfjhnyffkdnucigmisvalwfngwahy", "pzfkcxeehzytxrtbdqqcoaccnqiynsjypfrcablnznvmthga", "tlekyalvnyxsvr", "jsahgkicszfcheyqafvflufycicegxclqpbmvmakjbhtuwhcxerxwspgzsrubfqblbhavlwfhx", "tvzrvrsnjetkoxbliawcvzzakpmhhvnjubczcpgobuqjqffsrdxtxjarzgsuryajuatcyfbroginvjhlauzbrkhssf", "dqbrdlmkhkrdzecilmfuavrphuxbqjo", "pmdpypebaobezmcfazewjbabjqjvrefyravwrvneyfpesbwnbbymuvovjvqgfwmzcsweifbpkuzicpjsj", "wfobqlvkmehlrh", "soormzqclvrrhftinorqqtfprxqufqsadalczqgbdnirkohqvzucsuunzjvbcfrryzztgy", "lfpvzygoenywdaddtsnrtmdhqnrkuyydwbwmzbwxzmoxwxvyttomlqdsekhyoaltxjhdtggwxrueelgh", "sikbegcpidcsnbierikpueovdqaxzis", "bchfnjcxqkfavrpotyghvhdzfsnigpgeuen", "igcpbhexbrhqgvzbjuvgehqrntbdfluouysupmtykobvkxzhumwxtntmbnyditvjmgoqtbiabkzfx", "xlgxysycbrskihoxiolefjhgdluugsuvvlhxlbqxzcqgqoxrmhmrylpfbarrplqgpvep", "oivjponqitutsacujnlmwotbeinwfygnzdzqet", "yktuffealtkznzvccxuzejpruspfldidkheondsmdbgwrabkmwgtbguayqhyiynfjfqgbpxoizlhottxeqbauj", "sxghynxeletecuqbafmqmlmiwlaenuujfipfkeylcyfjxkznpkc", "gxekcfwgn", "zitnbxgfpnzdoaahksuajugypfdlzwzadmksgtuqoevnqjkwoybabozgovuzlazwlopcuofwnqpkcpyupnh", "oydlvwonusmqypnlpwhdakzdoft", "kphotdvwvjypd", "cdcwreimmvstaytgnlwrdzumkilseqrorlsgzmmogjixszokypjalqmexhjesknsnpgjowypsno", "bbfbbepyutonbnmtbmmdrgxnhoifxluendcayelykgoglnrucegkhoczbajaobdggmusdbdllz", "tmelbseagubuvjjvpzkyvbxcyxzoxjvhcqwmesdwi", "twbedagexfuyyghuqxgwxmtbckchmiknghoaswufihzcmcdkyiqphjwpgpqsavoiiejfoqoucmvcahxmshu", "aqnipfzpclcsyuecagdfho", "cfcigrykqaeclkckntqkioexxvzvqfyirgqiwsbamtigxewdgpbhxsrwiofxzakvofqzbznbkfpfqpnns", "uoyzgkluftocnmbdrpyrmaywyyevbfcebzfxqljgigcgfmoeznqpkbmiwmziqlmdealiknlfzdwbadyjunth", "pniwsxvfxlsfhkhijuwcdvzahgzjmxdjpggcmynlfeitrggpgfudhfsswqaxapmkalrmiikrdcqyovqqyiasc", "hjoff", "zchyafrvstjxrpcrjtxrzqtgemtnbioizjvrsbmmwsqfcqtovk", "rqopixeyjdtwekhdilymtmbvpllrbcfkfojwyzgvhlhxvflywzqiiqlnwooudmwjtfikdgecxsbwxnmmyaxkqyxbqsgn", "jrjjoqkpqfjslcklltztyhkmrqmlmvjczidsswyhmyowxuunogxomhxtireakdfheffjaarcmonvxxuhvlnpujenrvjpsdrngsif", "oluqeooumhfdizvbmzzxznccypdfqeibvhhbbknqcvdryuiqzmheivglczcxcneypm", "odiyjnibfvnmojenjmtnawnnsatslheoqliyqtsmrxojxlsqep", "xodhotsxujcannmgvmszwnpvlyxrvfntk", "pal", "pfuaarkhepsjjtljpdhne", "cmravobrplecnohwhfcxbgsmkqkqjmdrpz", "rhwlfbsjnoileeuatpwpzxsqssfxbakasvvhrnfvsnji", "cprpqsoavnarqcyjhuxeeapjwkbrignbhgkufpdbentazdgfrjksrmbvcbwypcrh", "ouxsbfsvuihlhjcsiccibxvxtpwuzhrthhotbinvclelxhujtxqnpzoylzatzgdvawmfjwychqeopcrdjnejdtfkdign", "zhoel", "jwcdwcwlljltoiepktkflngdsqeagqwowkbopjzwbruhzntusrixinwvf", "amn", "zpcniceupkondvpyjbckasmevmoxquumalzrnhpxskxugik", "cvefdfpkcctzpipaodrygoaoesgvihhhthygs", "bkohexejlvylwkrykmmcgcf", "olpytsjqlkdpwi", "qsidlbvlccnjtyrupfxprlwhcynoyxlbrxjojkiqrsawbaumnzkdkzchommc", "ixnpbwnjtpqczoybczviqfbjtuxjdibzgispvxdnjppranzldirjcxut", "zlzvegbitlzahbmkectimwpbaletlxpcotygzvwridoiswxiuawtz", "fhptimdesuoqzjiblcwnhhbaxptzraksykvfpunxfufrwcmvqnwjocoiy", "aiyorgqnkvdopapcjpenkymhnynpkzyntylozbydnpibdrpbvcutpxudrcsizoyk", "nmuyeikwomlcripugwjypkfvbvizvrmndhsacmdocjjdhtnqvuxmgrcfeczuxsa", "geulhswpnmyweuswggltxsguxnnvecovqxvteiuadxgdksmjtitwcpzmbxicwvclkndttsduxb", "tbdazbskpejjkquorhfdafdvlgglefjhqguriqaefpvhcqscfhtvkimqvjzxtaf", "ldnnyerfedgsffdhpbmbwuubrzsjibafxqccryuljvusadrlfxqw", "meylutslakssulbpvfonrfmvugblinwzrfgopxafnwwdcuufjuaimxrbheehjndxbiohskmvjrmpcewvvvrbaqbkfffgbb", "ygjmeswiarqiwkpuzjsfnbabtvgapaoazzkuafcpuuavzmvvhcnwedpgbizznkygooedxjggbdkrgkcyaautpvwwnvlfwikyl", "mjgnewabmklmzwazyhcdoyqowpgrdcnuzwoha", "nljceveqjwgwmylmbgkawzjdvueoglyxdbpdyqpgwgtmixlkxphtltijcm", "maunpmlrxvreydzpzllevzlxpbkeeyrsskuhtklrnmpytaugdnuvgotmptgprsfcrcenxg", "wxqdzhmffyesnlbweaipjgjojsvvfmyyrsxjuqgyseuojmsemhgxalbbdiepwbizkf", "ricbkdxnkujtlgnuzcx", "gttdwdoclpkcejljedytqilnrpgsphfetbxztwihmhhspvyfdtbavl", "srmlsxsnohyy", "lpg", "qpmfbycwecpgnthstqwcdwrfxlykjteonfqvgqfxgyjmnqfh", "guojktcpspmbtsfucyze", "uhkpgnmkewkhymqyfzxbb", "qifzafbdvmjgfmzdotznlbzsllakihgzdpmwliwmmyoyovofyhxupehqjryccvzsejvnfebbyaxtwji", "xifotcwgywruiymsgmkamaaqnawvooilhqflxptbiewtzcufqvlldsxapunlbbzurwlegvspxhkjnivmeuxu", "ewpkblzobahkxvmaorqxotmlszkjlnbbqhgfeoymxntpsilzzmgknzkpgwanlajbagyxdaxvgg", "xuxovygmkcyukmtsfwquiw", "otvowenq", "thfd", "upypegljhttngfmnrwoqunnysvaauywjppaqlqayfcpajnirjgwjmaeyfpkexyfhdztctsctgfyoyi", "pypgyhubkosgpcsiucjvocobhfiayxjydpjakwpduaybk", "zskfzaqcegxmcxjyyraaaskmurewhpdgeccdiqdruevkipfynevvgdwpmevxolczrssveyf", "qazabogdtspviktvigfumcliotxzlfgqdmnwnejdyuiaxdrhjpzpodjmk", "qozbqbokeous", "gwlvoonxjrhvzyqmnlwfykqex", "uaumnvenxonkofoquxkbevxhinefttwiymaeecwzlknmzljzcldmmpekrnfdql", "xzrghwbgmljjbboonroxerqkbuygiouaylhkdauoagnizfndqzabdbycwa", "wrmcoohqyiwguwxntxlaxlrcambhvughnujvqxtvxuiyvvbgdsqukkdrprcxk", "kajvhekqzmmolchwsnhwsotawzndwpoxxcphjxvjhwyqhzffyilcvwin", "osnjdwvjwtqplvovigvmkkuvzvyrnqbfpliyhvtbquiyucvfxzrvupjsaxisiqvdvjuavclfmbeddvype", "aa", "hzvczfzpteogbzckschfxooyybkkyodaoncwlfjrrozjedbniwhmrxbxgvsknpjtntia", "mnsplhfonlxqrfaoiqbroqrtvjdurciceuyazjslbxkznbjsaaiaiumshqmcdjrzkzkxskoiacnlwuumchanvp", "y", "llusbfpxolpcuxqlxsxusxljtvzoaukhebnimpaczhdgtlpdzmmxvqi", "cxxpgjxrfajufcyljmtomctdloifssgmqpdxnvnojtbczsunjirzmpavidwgvcrwioubusmrprda", "elyrktioorsgpmdogctdzoyumidyhkrcxjpsnqcjzqacpzsockdmpbobd", "cwklazpkfcycsdfjmjryxlikfpcjnjvjwckghqzhgcyhrfbfetmbakpnzjuvgmfngqluuxqvgprdmcclrdwkazvjciecidp", "jxgoqngvvdqgfzdtnlppzlpyxissnjpbcfvvdlzztbzspiwfglqbmvvqnpiscxzdmclnervesecqyyuppzeqnfzpddjn", "itprgrhysrwmhhaawatgzcoifzidwsmbubyigwcewvfsxipilgmyjoabnhovsr", "sdvroatgoiowdqxcchbkxuabrrxqrwqwaamebflbubghhdujyvjtrqjhs", "lcizmwl", "zbycvvwywvprknqomewgnukldcvzehmailqnpabcehjrsespzmcchlfiutwqhlcgvwwzgftqnsmmlxdot", "ycfajupfdilekjptftutzfultpjrupvajbtphxuxjacorflgh", "voeqeuorsrbostmivapuxevtmilhzxpeswivyiqkflxgdwpdieptpvplailynqsakhfnoluw", "qhaqtselyauycemuspozlctffeurfclumidaildhczgorkrvydqcraxhstswypcjktjzyijgbmozwfk", "cifmzmjvtuwzrvszvlkrfbxdztjtwttqxvwtrrdflwraygcnncq", "tqwdkiowbbeobixyurrlgzejkvrgpatwdfbkmexu", "qmohzsrkypncpinlhthrrjgalqhemwdliueqrnqbjqk", "jmuafzdxuyawoopckcihbgibuieaipjjuvelngmdwyhkthoeslmatibnxkexgiasuemzbbagijhuekilhguanvsi", "mcfttopijlwyiqjfddidkbrrznxiggspgrzv", "wkmpshoyvpcavisaluywwuxwggyguwyrewlsdxuhomkcipelifkzyremrdqnxriaddpzpqzfvdwyiuhkiyxlekpskkzawv", "qhcgluzepldgtcsyfquniwdy", "sqoxryxxgzqsjclmuvqiqawuidtihogggxjfzkniqjpyjwatpmecziomfgdkis", "istjakvtyzzaelduexpmnthzwgkwnhqtfdblilypkxirpnlnqrsjffthjdtbfwhxueboquotzvccnexwcre", "lrydfbliheallvpxasjfjfzeehxkxrgabcofnwetcjueolpgexetfieksdkeopci", "tptyttbimmijjnbciadfkwspfofbufpjuxcxylcbfdzoiqtsybxehtrryakqyptjkkkcsiqboreoiovoekelnx", "zavzklwqyzokxnunkhjylukjqcsrvlkuxs", "rtqyhcfhucrqadultzlumazicdu", "ohobdcictit", "xfganyznjxztlenimrljgyifwypjvmcxmjpdwfgspiyoyjetbrtrtxktiobhnzts", "fuvsdvoviyyzqzxliebnxpawtmczyiylnzvbyfilmbbgjoseyxbqawi", "kqtwdwcgtbgzmzejvskhevbokzdvomkawvqmsfwdhbnqdxlgy", "eplegvqtvywmbqtndpdcbbylonvowepnneqlywrtnryudbqorjasivuwkszxzfwqdqozrbubjuovkxehkenmlorvavznwoxbbj", "qjlynqakaxhvmbtzaymetjsilvmxfxjnlspufjxffwksiuyztzklwzvocatawrewicebujfgpq", "mghamvubjtiuviceyccpejehmumtaeejraargsbvfxsgbhigjejuvreuvgbjtzbrzmsrsgjhblzqoz", "zzvkiqrtyfssalidbudmgpjadfnvewcjxxeizrrkv", "wrsegnaznxixzhyhdmslgcgslrcmyvclh", "fujynwgcgocsbkzuvvppknspbvnitwbxnaf", "lihsicgcjuablnfflwuwifcrfwiioafxxbbqwgzibnwhoxzhvmhbgjafvcumjgthiqiuacojuycejodzghucnpogjyykzitugkcr", "wibifkhjooviuxbpucnreszrshiyfgcfywfncxiaodriejwrkskkwlgwxmzkrzfz", "pgmogxujiwkgusbriztdkzkoirswmxwmjjeomcbzg", "xzoksdragbjecrmropprjbtglqcsxzxaxfthhlkrcshobyisrlv", "gfgfhiljcgckutrrectpweexqmqntccqekojlbxsnvmkiareesbazlgsmunbagbmurgwzmb", "kldgnhosnhqdfpplqbpujzcpozokvyopbasgvkcevcwsrxjpgaksxybqsofsxzsdeflphvzpzqioflitssplfheepdraeketdj", "lrkykfzbqohzghvriwmwrtgptfdgwoclaehxujswufkmmuebvtozbhrjtpcmrwufrsjupoedgqsubbbvlbtkosrfdthrxbcq", "cuvkc", "qomgdgmepyktyexunnhirbqnarfdf", "rmvppwtfmeteylqons", "vdiqiavnkoxxqazrphttqhlevygfylfiyjkbmk", "nbeawufkicbfhwdohjmhalwxvnexleiarewaklpyrleygvqftnmtwhpxcbiajearbckhydjstlvwfeywdwcu", "fnwwldvonsrbqyfwecjclbodmzrgydejnsbshzbzosptoyugmvizkupntxffjzyijcjvnxhuzez", "imqfboyfcmbzjduerhkitmpopdaroi", "cukchstrkftlgefvyzpsujetytoebujmhwkipdgizdkipjcpvzzchqqrmqylmcszvemjbzgketpkzbrxguanvjbpxok", "bvyuoyqyryyeviocouoxdckpotoaklhalkxppvxwvzaxwh", "hkjlnprhizooehppsjxbsrbaxyuszevcukvoqffczvonstjiinxtwgzngctnkpbrpeohbxemlcjjgdlcvtfdodpqavcxftmnczk", "twzzzushasnqjxzfdtizajegksbhljakizojgausnhkxdfrypiriwmrraqnrhloctrvukkqons", "bizlpbpvdkcbcavxogrkaxmzbsujecqphusdxlhprrfokhbkgeapghdclkfgutfhkckoozolsyequrgnrndybvofuzca", "uhlgrpshm", "fffblhiue", "uxfxwhhhzwubnpkshnetkvmokzsnalh", "xzbqhsjmkkxkpreqdrooiifvkrxquxwlpytwbmespaqdknxcwfhnjesuwdujghoxznrlxelmi", "ljbdlnpjvzbiywyjqmxdxbqiwqkkvqslfklugpzithhviczc", "kqhattassqwlrxhpzaytmazlyfgvbglxgiqnrrghdjoclhhleqqlwdeblsviwkciqehcjypozryajlngeqrloxufpuryncat", "kyptacgghycdhhnhmlndjaqsrtjudnhwixqvuamkaguagzacczyirfqarfuklvjf", "tsuinonhxpreonbxzaztfeffdcymrxngqeuthmzojldodqlhpxpremc", "bvejqokgmvsxfugorsldmalka", "fxlloyqelywmhiwdtxvtttddthfzbajrgeznmxxgmwzvtmnnwjebeylwjspxygveswxtzefrfhlyhyyambt", "wuhqhyxfpsoaoyssbjwjxqzhckfyfsvonlmyphloyoagoxycirdriaramwxsrswbxptxzvdjticaacwnx", "vtgnhypjamsfxyvnzghqrmntherndth", "fmavnsloegrtcibrbifuibqvjvusppgmprqmibm", "prfyttezajzqbcccrhqdevnazibcpznkxouwegpbbapxlrmhkfhoihv", "hoaerkgvqbhssdrujbrtmlvwbnhqx", "ozerhqqdwfwduyxrjmtxrevbdtgmwpbhkxqjsnlssmestosukzlglqdfi", "gthdnp", "nukyujeknoixksgludcsmxbbsrqjbpnbtvuawtbihdo", "xflmwpjo", "eb", "uujqhzyytjv", "jgowebswpjsggcahajdxzlratrocfghnplpkabnzitmhfhwspgshvpamgqpilgortzvzuoexbaxcalxsxlzsdejgjgihulwxoabq", "sytmzzwifklxsrpyldonjbqchisdjfhpmhzjea", "eithdxqoyza", "sqozuxpdhuaqttpwvyilgjtrtttlmeabuxacxhhjmxfpunzdmxeghuukyutlfkk", "njsirzxawbzktvdyrhxzjkqgnipmjffsabonsdmkinjwqhmwngwcravsctcuglkk", "dmbkpmbvcklftvucfzjfagowavptxbezhrnkrvnfhauhfrdnfnxuneznqnjlwxxyhfcxrq", "cwdnifexgdpqqecqwktngwzqtqvbcywdphfxfnzoavileuflvkcnzirqdkpulrvmgjrqjblwfjdqacmwzytcqrxrspmlleolt"}, "owqugdlpqrnasodvbemfhuzctbibeboxgdklfyzyucomprzzoxwwxm"));
        System.out.println(countCharacters(new String[]{"cat", "bt", "hat", "tree"}, "atach"));
        System.out.println(countCharacters(new String[]{"hello", "world", "leetcode"}, "welldonehoneyr"));
    }

    public static int countCharacters(String[] words, String chars) {
        int result = 0;
        Map<Character, Integer> yy = new HashMap<>();
        char[] letters = chars.toCharArray();
        for (int i = 0; i < letters.length; i++) {
            if (yy.containsKey(letters[i])) {
                yy.put(letters[i], yy.get(letters[i]) + 1);
            } else {
                yy.put(letters[i], 1);
            }
        }
        for (int i = 0; i < words.length; i++) {
            char[] xx = words[i].toCharArray();
            Arrays.sort(xx);
            int count = 0;
            boolean allMatch = false;
            char target = xx[0];
            for (int j = 0; j < xx.length; j++) {
                if (target == xx[j]) {
                    count++;
                } else {
                    if (yy.containsKey(target)) {
                        if (yy.get(target) >= count) {
                            allMatch = true;
                            target = xx[j];
                            count = 1;
                        } else {
                            allMatch = false;
                            break;
                        }
                    } else {
                        allMatch = false;
                        break;
                    }
                }
            }
            if (allMatch) {
                System.out.println(words[i]);
                result += words[i].length();
            }
        }
        return result;
    }
}
